<?php
/**
 * 可乐会员操作
 * @desc 实现会员增加，扣除可乐币相关操作
 */
class Api_CocaCola_Vip extends PhalApi_Api {

    public function getRules() {
        return [
            "*"=>[
                "appid"=>['name' => 'appid', 'type' => 'string',  'require' => true, 'desc' => '应用id'],
                "secretkey"=>['name' => 'secretkey', 'type' => 'string', "explain"=>"s",  'require' => true, 'desc' => '应用秘钥']
            ],
            'username'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid']
            ],
            'no'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid']
            ],
            'birthday'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid']
            ],
            'credit'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid']
            ],
            'state'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid']
            ],
            'creditDel'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid'],
                'newCreit' => ['name' => 'newCreit', 'type' => 'int', 'min'=>1,  'require' => true, 'desc' => '需要扣除可乐币的数量']
            ],
            'creditAdd'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid'],
                'newCreit' => ['name' => 'newCreit', 'type' => 'int','min'=>1,  'require' => true, 'desc' => '需要增加可乐币的数量']
            ],
            'creditUpdate'  => [
                'openid' => ['name' => 'openid', 'type' => 'string',  'require' => true, 'desc' => 'openid'],
                'newCreit' => ['name' => 'newCreit', 'type' => 'int','min'=>1,  'require' => true, 'desc' => '具体可乐币的数量']
            ]
        ];
    }

    /**
     * 检查权限 todo
     * @param $appid
     * @param $secretkey
     * @return bool
     */
    protected function checkApp($appid,$secretkey){
        throw new PhalApi_Exception("无权限",10001);
        return false;

    }

    /**
     * 查询会员昵称
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return string data.username 用户昵称
     * @return string msg 提示信息
     */
    public function username() {
        $this->checkApp($this->appid,$this->secretkey,"username");

        $service= new Common_CocaColaService();
        $data=$service->Api("getMemberName",$this->openid);

        return [
            "username"=>$data
        ];
    }
    /**
     * 查询会员号
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return int data.NO 会员号
     * @return string msg 提示信息
     */
    public function no() {
        $this->checkApp($this->appid,$this->secretkey,"birthday");

        $service= new Common_CocaColaService();
        $data=$service->Api("getMemberNoOfOpenId",$this->openid);

        return [
            "NO"=>intval($data)
        ];
    }
    /**
     * 查询会员生日
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return date data.birthday 会员出生年月日
     * @return string msg 提示信息
     */
    public function birthday() {
        $this->checkApp($this->appid,$this->secretkey,"birthday");

        $service= new Common_CocaColaService();
        $data=$service->Api("getMemberAge",$this->openid);

        return [
            "birthday"=>$data
        ];
    }

    /**
     * 查询用户是否注册
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return boolean data.isReg 是否注册
     * @return string msg 提示信息
     */
    public function state() {
        $this->checkApp($this->appid,$this->secretkey,"state");

        $service= new Common_CocaColaService();
        $data=$service->Api("getMemberForState",$this->openid);

        return [
            "isReg"=>($data==1)
        ];
    }
    /**
     * 查询会员可乐币
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return int data.credit 会员现有可乐币
     * @return string msg 提示信息
     */
    public function credit() {
        $this->checkApp($this->appid,$this->secretkey,"credit");

        $service= new Common_CocaColaService();
        $data=$service->Api("getCreditOfOpenId",$this->openid);

        return [
            "credit"=>intval($data)
        ];
    }
    /**
     * 扣除会员可乐币
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return boolean data.isSuccess 扣币成功状态
     * @return string msg 提示信息
     */
    public function creditDel() {
        $this->checkApp($this->appid,$this->secretkey,"creditDel");

        $service= new Common_CocaColaService();
        $data=$service->Api("delCreditOfOpenId",["openId"=>$this->openid,"newCreit"=>$this->newCreit]);

        return [
            "isSuccess"=>($data==1)
        ];
    }

    /**
     * 增加会员可乐币
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return boolean data.isSuccess 加币成功状态
     * @return string msg 提示信息
     */
    public function creditAdd() {
        $this->checkApp($this->appid,$this->secretkey,"creditAdd");

        $service= new Common_CocaColaService();
        $data=$service->Api("addCreditOfOpenId",[$this->openid,$this->newCreit]);

        return [
            "isSuccess"=>($data==1)
        ];
    }
     /**
     * 设置会员可乐币
     * @desc
     * @return int ret HTTP状态码,200:成功
     * @return boolean data.isSuccess 设置成功状态
     * @return string msg 提示信息
     */
    public function creditUpdate() {
        $this->checkApp($this->appid,$this->secretkey,"creditUpdate");

        $service= new Common_CocaColaService();
        $data=$service->Api("updateCreditOfopenId",["openId"=>$this->openid,"newCreit"=>$this->newCreit]);

        return [
            "isSuccess"=>($data==1)
        ];
    }
}
